<?php

namespace app\http\middleware;

use app\facade\Token;
use app\common\exception\TokenException;
/**
 * CheckAuth
 */
class CheckAuth
{
    public function handle($request, \Closure $next)
    {
    	$route_info = $request->routeInfo();
    	$no_check_auth = [
    		'user/:version.Privilege/login' => 'login'
    		// 'user/:version.Privilege/logout' => 'logout'
    	];
    	if (isset($no_check_auth[$route_info['route']])) {
    		return $next($request);
    	}
    	$token = $request->header('access-token','');
    	if (empty($token)) {
    		throw new TokenException();
    	}
    	$result = Token::verifyToken($token);
    	if ($result === true) {
    		return $next($request);
    	}
    	throw new TokenException();
    }
}